package priv.mk.jdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class JDBCOOPUtils {
	private static final String packagename = "priv.mk.entry";
	private static Logger logger = Logger.getLogger(JDBCOOPUtils.class);

	public static void main(String[] args) {

		String sqlString = "select * from scott.emp where 1=2";
		String tableclassname = "question";
		generateEntry(sqlString, tableclassname);
	}

	private static void generateEntry(String sql, String tableclassname) {

		Map<String, String> dateMap = new HashMap<>();
		ResultSet set = null;
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		try {
			connection = DBUtil.getconn();// 调用工具类获取连接
			prepareStatement = connection.prepareStatement(sql);// 预编译SQL语句
			set = prepareStatement.executeQuery();
			ResultSetMetaData metaData = set.getMetaData();// 获取列信息
			for (int i = 1; i <= metaData.getColumnCount(); i++) {
				dateMap.put(metaData.getColumnName(i),
						metaData.getColumnClassName(i));// 获取列名 获取列的数据类型

			}
			FreemarkUtil.createEntryClassBody(dateMap, tableclassname,
					packagename);
			;
		}

		catch (Exception e) {// 异常处理
			e.printStackTrace();
		} finally {
			DBUtil.close(set, connection, prepareStatement);// 关闭
		}
	}
}